<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class wxpay extends API_Controller {

    function __construct(){
        parent::__construct();
        $this->_auto();
        $this->load->model('needs_model','mneeds');
        $this->load->model('pay_logs_model', 'mpay_log');
        $this->load->model('account_model','maccount');
        $this->load->model('need_order_model','mneedorder');
    }

    /**
     * @throws WxPayException
     * @content 悬赏支付押金（微信支付）
     * @生成微信支付签名
     */
    public function get_wxsign() {
        $this->_auto();
        $trade_no = $this->data['trade_no'];                                    //订单号
        $amount = $this->data['amount'];
        $subject = $this->data['subject'];
        if( !$trade_no || !$amount  ) {
            $this->vdata['returnCode'] = '250';
            $this->vdata['returnInfo'] = '缺少参数';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = array();
        }else{
            require_once LIBS_PATH."wxpaym/WxPayPubHelper.php";
            require_once LIBS_PATH. "wxpay/WxPay.Api.php";
            require_once LIBS_PATH."wxpay/log.php";
            //查询当前订单状态
            $input = new WxPayOrderQuery();
            $input->SetOut_trade_no($trade_no);
            $orderQuery = WxPayApi::orderQuery($input);
            if (isset($orderQuery['trade_state']) && ($orderQuery['trade_state'] == 'SUCCESS')) {
                $this->vdata['returnCode'] = '250';
                $this->vdata['returnInfo'] = '该订单已经支付';
                $this->vdata['secure'] = JSON_SECURE;
                $this->vdata['content'] = array();
            }else{
                //初始化日志
                $logHandler= new CLogFileHandler(LOG_PATH.'wxpay'.date('Y-m-d').'.log');
                $log = Log::Init($logHandler, 15);
                $notify = new Notify_pub();
                $notifyUrl = site_url('notify/wxApi');                          //回调地址
                $paydata=array(
                    'appid'=>WxPayConf_pub::APPID,
                    'mch_id'=>WxPayConf_pub::MCHID,
                    'nonce_str'=>$notify->createNoncestr(),
                    'body'=>$subject ? $subject : '支付订单',
                    'out_trade_no'=>$trade_no,
                    'total_fee'=>$amount * 100,
                    'spbill_create_ip'=>get_ip(),
                    'notify_url'=>$notifyUrl,
                    'trade_type'=>'APP'
                );
                $paydata['sign']=$notify->getSign($paydata);
                Log::DEBUG("微信sign:");
                Log::DEBUG(print_r($paydata,1));
                $xml= $notify->postXmlCurl($notify->arrayToXml($paydata),'https://api.mch.weixin.qq.com/pay/unifiedorder');
                Log::DEBUG("微信xml:");
                Log::DEBUG(print_r($xml,1));
                $paydatanew=$notify->xmlToArray($xml);
                $outparams = array();
                if($paydatanew['return_code']=="SUCCESS"){
                    $pack   = 'Sign=WXPay';
                    $outparams['appid']=WxPayConf_pub::APPID;
                    $outparams['noncestr']=$paydata['nonce_str'];
                    $outparams['package']=$pack;
                    $outparams['partnerid']=WxPayConf_pub::MCHID;
                    $outparams['prepayid']=$paydatanew['prepay_id'];
                    $outparams['timestamp']=time();
                    //生成签名
                    ksort($outparams);
                    reset($outparams);
                    $outparams['sign'] = $notify->getSign($outparams);
                    echo json_encode($outparams);
                }
            }
        }

    }

}